import 'dart:async';
import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:mybase/base/tool/empty/FFEmpty.dart';
import 'package:myuis/constant/PackageValue.dart';
import 'package:myuis/language/LanguageEntity.dart';


const String CODELINE = "-";
const String CCODE_CN = "CN";

///中文
const String LCODE_ZH = "zh";
///英文
const String LCODE_EN = "en";
///德语
const String LCODE_DE = "de";
///日语
const String LCODE_JA = "ja";
///韩文
const String LCODE_KO = "ko";
///法语
const String LCODE_FR = "fr";
///西班牙
const String LCODE_ES = "es";
///意大利
const String LCODE_IT = "it";
///印度尼西亚语
const String LCODE_ID = "id";
///俄语
const String LCODE_RU = "ru";
///葡萄牙
const String LCODE_PT = "pt";
///哈萨克
const String LCODE_KK = "kk";
///乌克兰
const String LCODE_UK = "uk";
///泰语
const String LCODE_TH = "th";
///土耳其
const String LCODE_TR = "tr";
///波兰
const String LCODE_PL = "pl";
///越南语
const String LCODE_VI = "vi";
///印地语
const String LCODE_HI = "hi";

const String SCODE_HANS = "Hans";
const String SCODE_HANT = "Hant";

class LanguageConfig {

  ///支持的语言版本
  static List<LanguageConfigEntity> supportedLocalesList = [
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_EN), languageName: 'English'),
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_ZH, scriptCode: SCODE_HANS), languageName: '简体中文'),
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_ZH, scriptCode: SCODE_HANT), languageName: '繁體中文'),

    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_DE), languageName: 'Deutsch'),
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_JA), languageName: '日本語'),
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_KO), languageName: '한국인'),
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_FR), languageName: 'Français'),
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_ES), languageName: 'español'),
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_IT), languageName: 'italiano'),
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_ID), languageName: 'bahasa Indonesia'),
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_RU), languageName: 'Русский'),
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_PT), languageName: 'Português'),
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_KK), languageName: 'қазақ'),
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_UK), languageName: 'українська'),
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_TH), languageName: 'แบบไทย'),
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_TR), languageName: 'Türkçe'),
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_PL), languageName: 'Polski'),
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_VI), languageName: 'Tiếng Việt'),
    LanguageConfigEntity(locale: const Locale.fromSubtags(languageCode: LCODE_HI), languageName: 'हिंदी'),
  ];

  static Iterable<Locale> supportedLocales() {
    List<Locale> list = [];
    for (int i = 0; i < supportedLocalesList.length; i++) {
      LanguageConfigEntity entity = supportedLocalesList[i];
      list.add(entity.locale);
    }
    return list;
  }

}

///多语言类
class AppuisLocale {
  final Locale locale;
  LanguageEntity? language;

  AppuisLocale(this.locale, this.language);

  static const AppuisLocaleDelegate delegate = AppuisLocaleDelegate();

  static AppuisLocale? of(BuildContext context) {
    return Localizations.of(context, AppuisLocale);
  }
}

///多语言代理类
class AppuisLocaleDelegate extends LocalizationsDelegate<AppuisLocale> {
  const AppuisLocaleDelegate();

  @override
  bool isSupported(Locale locale) {
    List<String> list = [];
    for (int i = 0; i < LanguageConfig.supportedLocalesList.length; i++) {
      LanguageConfigEntity entity = LanguageConfig.supportedLocalesList[i];
      if (!list.contains(entity.locale.languageCode)) {
        list.add(entity.locale.languageCode);
      }
    }
    return list.contains(locale.languageCode);
  }

  //加载自定义资源 转成Model
  @override
  Future<AppuisLocale> load(Locale locale) async {

    String languageCode = locale.languageCode;
    String? scriptCode = locale.scriptCode;

    String addScriptCode = "";
    if (!FFEmpty.stringIsEmpty(scriptCode)) {
      addScriptCode = "$CODELINE$scriptCode";
    }

    //在包里面读取包的本地配置
    String jsonStr = await rootBundle.loadString("packages/${PackageValue.package}/assets/uislanguage/$languageCode$addScriptCode.json");
    var map = json.decode(jsonStr);
    LanguageEntity languageEntity = LanguageEntity.fromJson(map);

    return SynchronousFuture<AppuisLocale>(AppuisLocale(locale, languageEntity));
  }

  @override
  bool shouldReload(LocalizationsDelegate<AppuisLocale> old) {
    return false;
  }

}

class LanguageConfigEntity {
  Locale locale;
  String languageName;


  LanguageConfigEntity({required this.locale, required this.languageName,});
}